package com.shine.chemicalWholeProcessSystem.ui.login;

import android.Manifest;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;

import com.google.gson.Gson;
import com.shine.chemicalWholeProcessSystem.R;
import com.shine.chemicalWholeProcessSystem.app.Constants;
import com.shine.chemicalWholeProcessSystem.base.BaseActivity;
import com.shine.chemicalWholeProcessSystem.bean.LoginBean;
import com.shine.chemicalWholeProcessSystem.databinding.ActivityLoginBinding;
import com.shine.chemicalWholeProcessSystem.http.ApiException;
import com.shine.chemicalWholeProcessSystem.http.ECHttpResponse;
import com.shine.chemicalWholeProcessSystem.http.ECObserver;
import com.shine.chemicalWholeProcessSystem.http.RetrofitClient;
import com.shine.chemicalWholeProcessSystem.http.RxUtils;
import com.shine.chemicalWholeProcessSystem.http.Service;
import com.shine.chemicalWholeProcessSystem.ui.main.MainActivity;
import com.shine.chemicalWholeProcessSystem.utils.SPUtils;
import com.shine.chemicalWholeProcessSystem.utils.SystemUtil;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.functions.Consumer;

public class LoginActivity extends BaseActivity<ActivityLoginBinding, LoginActivityViewModel> {

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_login;
    }

    @Override
    public int initVariableId() {
        return com.shine.chemicalWholeProcessSystem.BR.viewModel;
    }

    @Override
    public LoginActivityViewModel initViewModel() {
        return new LoginActivityViewModel(this);
    }

    @SuppressLint("CheckResult")
    @Override
    public void initData() {
        super.initData();
        setStatusBar();

        binding.phone.setText(SPUtils.getInstance().getString(Constants.SP_LOGIN_NAME, ""));
        binding.code.setText(SPUtils.getInstance().getString(Constants.SP_LOGIN_PASS, ""));
        //清楚所有本地数据
//        SPUtils.getInstance().clear();
        binding.login.setOnClickListener(this);

        RxPermissions mRxPermission = new RxPermissions(this);
        mRxPermission.request(Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean) {

                        } else {
                        }
                    }
                });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login:
                if (binding.phone.getText().toString().isEmpty()) {
                    ToastMessage("请输入您的账号");
                    return;
                }

                if (binding.code.getText().toString().isEmpty()) {
                    ToastMessage("请输入您的登录密码");
                    return;
                }
//                switchActivityAndFinish(MainActivity.class);
                loginByPassword();
                break;
            default:
                break;
        }
    }


    private void loginByPassword() {
        LoginBean loginBean = new LoginBean();
        loginBean.setUsername(binding.phone.getText().toString());
        loginBean.setPassword(SystemUtil.md5(binding.code.getText().toString()));

        showLoading("");
        RetrofitClient.getInstance().create(Service.class)
                .login(new Gson().toJson(loginBean), SystemUtil.getPhoneSign(this))
                .compose(RxUtils.<ECHttpResponse<LoginBean>>bindToLifecycle(this))
                .compose(RxUtils.<ECHttpResponse<LoginBean>>schedulersTransformer())
                .compose(RxUtils.<ECHttpResponse<LoginBean>>exceptionTransformer())
                .subscribe(new ECObserver<LoginBean>() {
                    @Override
                    protected void _onNext(LoginBean loginBean) {
                        dismissLoading();
                        SPUtils.getInstance().put(Constants.SP_LOGIN_NAME, binding.phone.getText().toString());
                        SPUtils.getInstance().put(Constants.SP_LOGIN_PASS, binding.code.getText().toString());
                        SPUtils.getInstance().put(Constants.SP_TOKEN, loginBean.getBaseinfo().getToken());
                        SPUtils.getInstance().put(Constants.SP_MARK, loginBean.getBaseinfo().getLoginMark());
                        switchActivityAndFinish(MainActivity.class);
                    }

                    @Override
                    protected void _onError(ApiException ex) {
                        ToastMessage(ex.message);
                        dismissLoading();
                    }
                });
    }


}
